﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class Unit : TAM_SPORT.FormBase
    {
        UnitDao dao;
        public Unit()
        {
            InitializeComponent();
            dao = new UnitDao();
            unit();
        }

        private void unit()
        {
            dtUnit.Rows.Clear();
            DataTable table = dao.getUnits("");
            if (table != null && table.Rows.Count >= 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtUnit.Rows.Add();
                    dtUnit.Rows[i].Cells[0].Value = i + 1;
                    dtUnit.Rows[i].Cells[1].Value = row1["NAME"].ToString();
                    dtUnit.Rows[i].Cells[2].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtUnit.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() == "")
            {
                Error er = new Error(UserConstant.INPUT_VALIDATE);
                er.ShowDialog();
                er.Dispose();
            }
            else
            {
                if (dao.insertUnit(txtName.Text.Trim()))
                {
                    Result re = new Result(UserConstant.SAVE_SUCCESSFUL);
                    re.ShowDialog();
                    re.Dispose();
                    unit();
                    txtName.Text = "";
                }
                else
                {
                    Error er = new Error(UserConstant.SAVE_FAIL);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dtUnit_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void dtUnit_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                int id = int.Parse(dtUnit.Rows[e.RowIndex].Cells[2].Value.ToString());
                EditUnit er = new EditUnit(id);
                er.ShowDialog();
                unit();
            }
            else if (e.RowIndex >= 0 && e.ColumnIndex == 4)
            {
                int id = int.Parse(dtUnit.Rows[e.RowIndex].Cells[2].Value.ToString());
                Confirm co = new Confirm("Bạn có chắc muốn xóa đơn vị này?");
                co.ShowDialog();
                if (co.IsAccept)
                {
                    if (dao.deleteUnit(id))
                    {
                        Result re = new Result("Xóa thành công.");
                        re.ShowDialog();
                        re.Dispose();
                        unit();
                    }
                    else
                    {
                        Error er = new Error("Xóa thất bại");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
            }

        }
    }
}
